package fr.p10.miage.gestionabsence.action;

import fr.p10.miage.gestionabsence.form.AbsenceNoteActionForm;
import fr.p10.miage.gestionabsence.form.LoginActionForm;
import fr.p10.miage.gestionabsence.model.*;
import java.io.File;
import java.io.FileInputStream;
import java.net.InetAddress;
import java.sql.SQLException;
import java.sql.Time;
import java.util.Calendar;
import java.util.Date;
import java.util.Properties;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Sofian Seid
 */
public class AbsenceNoteAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    /**
     * This is the action called from the Struts framework.
     *
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.sql.SQLException
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws  SQLException, Exception {
        
        ActionErrors errors = new ActionErrors();
        Boolean presence = true;
        String ipSalleDebut="";
        String ipSalleFin="";
        AgendaBDD abdd = new AgendaBDD();
        PresenceBDD pbdd = new PresenceBDD();
        AgendaAffichage agenda = abdd.getAgendaIntervenantById(Integer.parseInt(request.getParameter("id")));
        Etudiant e = SessionUtilisateur.getEtudiant(request);
        
        
        Calendar now = Calendar.getInstance();

        int heure = now.get(Calendar.HOUR_OF_DAY);
        int minute = now.get(Calendar.MINUTE);

        
        
        String heureDebut = agenda.getEcDebut();
        String heureFin = agenda.getEcFin();
         
        try {
             presence = pbdd.trouverParEtudiantetAgenda(e.getId(),Integer.parseInt(request.getParameter("id")));             
        } catch (Exception ex) {
            errors.add("compte", new ActionMessage("errors.invalid", " Identifiant/Mot de passe"));
        }


        InetAddress adresseIp =InetAddress.getLocalHost();
        adresseIp.getHostAddress();
//        switch (agenda.getAgendaSalle()) {
//            case "210-1":
//                ipSalleDebut = "32";
//                ipSalleFin = "54";
//                ipSalleDebut = "20";
//                ipSalleFin = "34";
//                
//                break;
//            case "210-2":
//                ipSalleDebut = prop.getProperty("salle210_2Debut");               
//                ipSalleFin = prop.getProperty("salle210_2Fin");
//                break;
//            default:
//                presence = true;
//                break;
//        }
       
        
       
        if (presence)
        {
            errors.add("Identification", new ActionMessage("errors.verif", "Vous êtes dèja noté présent"));
           if(!errors.isEmpty()){
                this.saveErrors(request, errors);
            }
            return mapping.findForward("failed");
        }
        else if(!CompareHeure(heureDebut,heureFin,heure,minute))
        {
            errors.add("horaire", new ActionMessage("errors.verif", "Vous ne pouvez plus vous signer présent"));
            if(!errors.isEmpty()){
                this.saveErrors(request, errors);
            }
            return mapping.findForward("failed");
        }
        else
        {
           PresenceBDD.ajouter(e.getId(),agenda.getIdAgenda());
           return mapping.findForward("QUESTION");
        }
    }
    private boolean CompareHeure(String horaireDebut,String horaireFin, long heureNow, long minuteNow)
    {
       long newHeureDebut = Extract(horaireDebut);
       long newHeureFin = Extract(horaireFin);
       
       String horaireNow =   Long.toString(heureNow) +  Long.toString(minuteNow) ;
       long horaire = Long.parseLong(horaireNow);
       
       if(newHeureDebut <= horaire && newHeureFin >= horaire)
       {
           return true;
       }
       return false; 
    }
    
    private long Extract(String horaire)
    {
       String [] myTab = horaire.split("H");
       return Long.parseLong(myTab[0]+myTab[1]); 

    }

}